package com.taru.university.controller;

import com.taru.university.model.Photo;
import com.taru.university.service.PhotoService;
import com.taru.university.utils.Constant;
import com.taru.university.utils.JsonResult;
import com.taru.university.model.Exchange;
import com.taru.university.service.ExchangeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
public class ExchangeController {
    private static Logger logger = LoggerFactory.getLogger(ExchangeController.class);
    @Autowired
    ExchangeService exchangeService;

    @RequestMapping("/query/exchange")
    @ResponseBody
    public Object queryExchange(@RequestParam(value = "pageNum") int pageNum,
                                @RequestParam(value = "pageSize")   int pageSize){
        JsonResult result = null;
        try {
            List<Exchange> exchangeList = exchangeService.queryExchange(pageNum,pageSize);
            logger.info("查看交流表");
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,exchangeList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/exchange/len")
    @ResponseBody
    public Object queryExchangeLen(){
        JsonResult result = null;
        try {
            List<Exchange> exchangeList = exchangeService.queryExchangeLen();
            logger.info("查看交流表");
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,exchangeList);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/query/exchange/id")
    @ResponseBody
    public Object queryExchangeById(String exchangeId){
        JsonResult result = null;
        try {
            Exchange exchange = exchangeService.queryExchangeById(exchangeId);
            logger.info("交流表Id：{}", exchangeId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS,exchange);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/insert/exchange")
    @ResponseBody
    public Object insertExchange(Exchange exchange){
        JsonResult result = null;
        try {
            exchangeService.insertExchange(exchange);
            logger.info("添加交流表：{}", exchange);
            result = new JsonResult("添加成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("添加异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/delete/exchange")
    @ResponseBody
    public Object deleteExchange(String exchangeId){
        JsonResult result = null;
        try {
            exchangeService.deleteExchange(exchangeId);
            logger.info("交流表Id：{}", exchangeId);
            result = new JsonResult("删除成功", Constant.STATUC_SUCCESS,exchangeId);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("删除异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

}
